package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        //判断密码是否为空
        String password = member.getPassword();
        if(password!=null){
            //不等于空需要加密处理
            member.setPassword(MD5Utils.md5(password));
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        //创建一个集合容器
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            Integer memberCountBeforeDate = memberDao.findMemberCountBeforeDate(month + ".31");
            memberCount.add(memberCountBeforeDate);
        }
        return memberCount;
    }
}
